﻿Imports System.Data.SqlClient
Imports System.Xml
Public Class Frm_SanPham

    Dim clsdm As New Cls_DanhMuc
    Private Sub Frm_SanPham_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        clsdm.FillCombo(Me.cbxtenloaisp, "select * from LoaiSP", "TenLoaisp", "Maloaisp")
        clsdm.FillCombo(Me.cbxdvt, "select * from Donvitinh", "TenDvt", "Madvt")
        FillData()
    
    End Sub
    Private Sub FillData()
        clsdm.FillDataGirdView("select masp,tensp,tenloaisp,dongia,tendvt,mota from SanPham a,loaisp b,donvitinh c where a.maloaisp=b.maloaisp and c.madvt=a.madvt", Me.dtg, Me.dts)
    End Sub
    Function KiemTraTextBox() As Integer
        Dim str As String = ""
        If Me.txttensp.Text.Trim = "" Then
            str += "Bạn chưa nhập tên Hàng " & Chr(13)
        End If
        If Me.cbxtenloaisp.Text = "   --- Chọn --- " Then
            str += "Bạn chưa Loại Sản phẩm " & Chr(13)
        End If
        If txtdongia.Text <> "" Then
            If IsNumeric(txtdongia.Text) = False Then
                MsgBox("Bạn nhập Sai Đơn Giá")
                txtdongia.Focus()
                Return False
            End If
        Else
            Me.txtdongia.Text = 0
        End If
        If str <> "" Then
            MsgBox("Thông tin cần nhập:" & Chr(13) & Chr(13) & str, MsgBoxStyle.Information, "Thông Báo")
            Return 0
        Else
            Return 1
        End If
    End Function
    Private Sub btthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthem.Click
        If Me.btthem.Text = "&Thêm" Then
            Me.btthem.Text = "&Hủy"
            clsdm.FillDataText(Me.txtmasp, "Tang_Masp")
            Me.btluu.Enabled = True
            Me.btsua.Enabled = False
            Me.btxoa.Enabled = False
            Me.txttensp.Text = ""
            Me.txttensp.Focus()

        Else
            Me.btthem.Text = "&Thêm"
            Me.txtmasp.Text = ""
            Me.btsua.Enabled = True
            Me.btxoa.Enabled = True

        End If
    End Sub
    Private Sub btluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btluu.Click
        Dim mycmd As New SqlCommand("ThemSP", con)
        mycmd.CommandType = CommandType.StoredProcedure
        mycmd.Parameters.Add("@masp", SqlDbType.NVarChar).Value = txtmasp.Text
        mycmd.Parameters.Add("@maloaisp", SqlDbType.Int).Value = cbxtenloaisp.SelectedValue
        mycmd.Parameters.Add("@Madvt", SqlDbType.Int).Value = cbxdvt.SelectedValue
        mycmd.Parameters.Add("@tensp", SqlDbType.NVarChar).Value = txttensp.Text
        mycmd.Parameters.Add("@dongia", SqlDbType.Int).Value = Format(CInt(txtdongia.Text))
        mycmd.Parameters.Add("@Mota", SqlDbType.NVarChar).Value = txtmota.Text
     
        Try
            mycmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox("Lỗi Cập Nhật " & ex.Message, MsgBoxStyle.Critical, "Thông Báo")
        End Try
        Me.btluu.Enabled = False
        Me.btsua.Enabled = True
        Me.btxoa.Enabled = True
        Me.btthem.Text = "&Thêm"
        FillData()
    End Sub
    Private Sub btxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btxoa.Click
        Dim strma, strten As String
        strma = dtg.CurrentRow.Cells(0).Value.ToString()
        strten = dtg.CurrentRow.Cells(1).Value.ToString()
        If MsgBox("Bạn có chắc xóa Mã Sản Phẩm: " & strma & "Có tên: " & strten, MsgBoxStyle.YesNo, "Thông Báo") = MsgBoxResult.Yes Then
            Try
                Dim sqlCmd As New SqlCommand("XoaSP", con)
                sqlCmd.CommandType = CommandType.StoredProcedure
                sqlCmd.Parameters.Add("@masp", SqlDbType.Char).Value = Me.txtmasp.Text.Trim
                sqlCmd.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox("Lỗi khi xóa" & ex.Message)
                Exit Sub
            End Try
        End If
        Me.txtmasp.Text = ""
        Me.txttensp.Text = ""
        Me.txtdongia.Text = ""
        FillData()
    End Sub
    Private Sub btsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btsua.Click
        If KiemTraTextBox() = 1 Then
            Try
                Dim cmd As New SqlCommand("SuaSP", con)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@Masp", SqlDbType.NVarChar).Value = txtmasp.Text.Trim
                cmd.Parameters.Add("@MaLoaisp", SqlDbType.Int).Value = cbxtenloaisp.SelectedValue
                cmd.Parameters.Add("@Madvt", SqlDbType.Int).Value = cbxdvt.SelectedValue
                cmd.Parameters.Add("@Tensp", SqlDbType.NVarChar).Value = txttensp.Text.Trim
                cmd.Parameters.Add("@Dongia", SqlDbType.Money).Value = Format(CInt(txtdongia.Text.Trim))
                cmd.Parameters.Add("@Mota", SqlDbType.NVarChar).Value = txtmota.Text
                cmd.ExecuteNonQuery()
                FillData()
            Catch ex As Exception
                MsgBox("Lỗi khi Sửa" & ex.Message)
                Exit Sub
            End Try
        End If
    End Sub
    Private Sub btthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthoat.Click
        Me.Close()
    End Sub

    Private Sub dtg_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtg.SelectionChanged
        Me.txtmasp.Text = dtg.CurrentRow.Cells(0).Value.ToString()
        Me.txttensp.Text = dtg.CurrentRow.Cells(1).Value.ToString()
        Me.cbxtenloaisp.Text = dtg.CurrentRow.Cells(2).Value.ToString()
        Me.txtdongia.Text = dtg.CurrentRow.Cells(3).Value.ToString()
        Me.cbxdvt.Text = dtg.CurrentRow.Cells(4).Value.ToString()
        Me.txtmota.Text = dtg.CurrentRow.Cells(5).Value.ToString()
    End Sub

    Private Sub txtdongia_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtdongia.TextChanged
        'If IsNumeric(Me.txtdongia.Text.Trim) = True And Len(Me.txtdongia.Text) > 3 Then
        '    Me.txtdongia.Text = Format(CInt(Me.txtdongia.Text), "#,###")
        'End If
    End Sub
End Class